﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Timer = System.Threading.Timer;

namespace Turning_Lines
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private const int AmountLines = 354;
        private readonly List<Line> _panelLines;
        private int _startIndex = AmountLines / 2;
        private int _middleIndex1 = AmountLines/2;
        private const double AnimationSeconds = 3.1;

        public MainWindow()
        {

            InitializeComponent();
            _panelLines = new List<Line>();
            LoadLines();
            var tmr = new DispatcherTimer()
            {
                Interval = TimeSpan.FromSeconds(0.1)
            };
            tmr.Tick += tmr_Elapsed;
            tmr.Start();
        }

        private void tmr_Elapsed(object sender, EventArgs e)
        {
            //Rotate start = Yx6
            for (int i = _startIndex+1; i < _startIndex + 7; i++)
            {
                var rotateAnimation = new DoubleAnimation(0, 180, TimeSpan.FromSeconds(AnimationSeconds));
                rotateAnimation.EasingFunction = new SineEase();
                var rt = (RotateTransform)_panelLines[i].RenderTransform;
                rt.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation);
            }

            for (int i = _middleIndex1 ; i > _middleIndex1-6; i--)
            {
                var rotateAnimation = new DoubleAnimation(0, -180, TimeSpan.FromSeconds(AnimationSeconds));
                rotateAnimation.EasingFunction = new SineEase();
                var rt = (RotateTransform)_panelLines[i].RenderTransform;
                rt.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation);
            }


            if (_startIndex < AmountLines-10)
                _startIndex += 6;
            else
                _startIndex = AmountLines / 2;


            if (_middleIndex1 > 10)
                _middleIndex1 -= 6;
            else
                _middleIndex1 = AmountLines/2;

        }



        private void LoadLines()
        {
            var transformGroup = new TransformGroup();
            transformGroup.Children.Add(new ScaleTransform());
            transformGroup.Children.Add(new SkewTransform());
            transformGroup.Children.Add(new RotateTransform() { Angle = 0 });
            transformGroup.Children.Add(new TranslateTransform());

            for (int i = 0; i < AmountLines; i++)
            {
                var line = new Line()
                {
                    Name = string.Format("Line{0}", i),
                    X1 = 25,
                    Y1 = 50,
                    X2 = 25,
                    Y2 = 0,
                    Stroke = Brushes.Black,
                    StrokeThickness = 2,
                    RenderTransformOrigin = new Point(1, 0.5),
                    RenderTransform = new RotateTransform() { Angle = 0 },
                };
                _panelLines.Add(line);
                Panel.Children.Add(line);
            }
        }
    }
}
